/**
 * Created by xinguoren on 15-4-16.
 */
var localUrls = require("../utils/urls");
var MongoClient = require('mongodb').MongoClient;


function getConnect(dbName, callback) {
    MongoClient.connect(localUrls.mongodb + "/" + dbName, function (err, db) {
        if (err) {
            console.log(dbName+" - mongodb连接异常", err);
            callback(null);
            if (!!db){
                db.close();
            }

        } else {
            console.log("成功连接mongodb-            ",dbName);
            callback(db);
        }
    });
};

exports.insertMongo = function(query, callback){
    var dbName = query.dbName ? query.dbName :localUrls.mongodb_default;
    getConnect(dbName,function(db){
        if(!!db){
            console.log('即将插入数据：',query);
            var collection = db.collection(query.collection);
            collection.insert(query.insert, function(err, result){
                if(err){
                    console.log("mongodb插入数据失败, 信息：",err);
                    callback("insert error");
                }else{
                    console.log("mongodb插入数据成功");
                    callback(result);
                }
                db.close();
            });
        }else{
            callback("insert error");
        }
    })
};

exports.search = function(query, callback){
    var dbName = query.dbName ? query.dbName : localUrls.mongodb_default;

    getConnect(dbName,function(db){
        if(!!db){
            console.log('mongo查询条件数据：',query);
            var collection = db.collection(query.collection);
            collection.find(query.search).toArray(function(err, result){
                if(err){
                    console.log("mongodb查询数据失败, 信息：",err);
                    callback("search error");
                }else{
                    console.log("mongodb查询数据成功",result);
                    callback(result);
                }
                db.close();
            });
        }else{
            callback("search error");
        }
    })
};

exports.deleteMongo = function(query, callback){
    var dbName = query.dbName ? query.dbName : localUrls.mongodb_default;

    getConnect(dbName,function(db){
        if(!!db){
            console.log('mongo删除数据：',query);
            var collection = db.collection(query.collection);
            collection.remove(query.delete, function(err, result){
                if(err){
                    console.log("mongodb删除数据失败, 信息：",err);
                    callback("delete error");
                }else{
                    console.log("mongodb删除数据成功",result);
                    callback(result);
                }
                db.close();
            });
        }else{
            callback("delete error");
        }
    })
};

exports.findandmodify = function(query, callback){
    var dbName = query.dbName ? query.dbName :localUrls.mongodb_default;
    getConnect(dbName,function(db){
        if(!!db){
            console.log('即将插入数据：',query);
            var collection = db.collection(query.collection);
            collection.findAndModify(query.findAndModify, function(err, result){
                if(err){
                    console.log("mongodb查询修改数据失败, 信息：",err);
                    callback("findandmodify error");
                }else{
                    console.log("mongodb查询修改数据成功",result);
                    callback(result);
                }
                db.close();
            });
        }else{
            callback("findandmodify error");
        }
    })
};